<template>
    <rankingFlow :pop="pop" :loading='loading' :rankTop8="rankTop8" :loadingText="loadingText" >
      <slot ></slot>
    </rankingFlow>
</template>
<style lang="scss">
</style>
<script>
let moment = require("moment"); 
import rankingFlow from "@/components/real-time/real-time-module/common/rankingFlow";
export default {
  components: {
      rankingFlow
  },
  props:{
    GetData:{},
    isdemo:{
      type:Boolean,
      default:function(){
        return false
      }
    }
  },
  data(){
    return {
      pop:{
          title: "案场客流排行",							//报表标题
          helphint:[
                  {title:"案场客流排行",text:""},				//第一条信息为提示标题 不给text
                  {title:"案场客流排行",text:"：根据进入人数取前8名"},
          ]
      },
      realData: {
        company_ids: '',//企业id
        case_ids: '',//案场id
        idsType: 0,//企业或案场标记：0：为只选择了企业id，1：为选择了案场id
        start_day: moment(new Date()).format("YYYY-MM-DD"),
        end_day: moment(new Date()).format("YYYY-MM-DD"),
      },
      rankTop8:[],
      loading:false,
      loadingText:'加载中...',
      start_hm:'00:00',
      end_hm:'23:59',
      global:null,
      xykpis:['enters'],
      shake:false
    }
  },
  created() {
    if(this.isdemo){  
      this.rankTop8=[
        {
          name: "云视北京店",
          unit: "人",
          value: 88888
        },
        {
          name: "云视上海店",
          unit: "人",
          value: 78888
        },
        {
          name: "云视广州店",
          unit: "人",
          value: 68888
        },
        {
          name: "云视深圳店",
          unit: "人",
          value: 58888
        },
        {
          name: "云视安徽店",
          unit: "人",
          value: 48888
        },
        {
          name: "云视杭州店",
          unit: "人",
          value: 46666
        },
        {
          name: "云视香港店",
          unit: "人",
          value: 38888
        },
        {
          name: "云视云南店",
          unit: "人",
          value: 28888
        }
      ]
      this.loading=false;
    }
  },
  mounted() {},
  beforeDestroy(){},
  methods: {
    getData(){
      let tags=0;
      if(this.global.type=='chain')
      {
        tags=2;
      }
      else if(this.global.type=='mall')
      {
        tags=1;
      }
      let ids = this.realData.idsType ? this.realData.case_ids : this.realData.company_ids
      let kpis=this.xykpis.join(',');
      let url='/sys/report_new/ranking_sum?group_type=case&tags='+tags+'&ranking_num=8&indicator_key='+kpis+'&start_day='+this.realData.start_day+'&end_day='+this.realData.end_day
      +'&start_hm='+this.start_hm+'&end_hm='+this.end_hm;
      let idarr=ids.split(',');
      let parmas={
        org_id_list:idarr,
        org_type:this.realData.idsType+1
      };
      this.$api.post(url,parmas)
      .then(data=>{
        if(data&&data.data){
          if(data == '10001'){
            this.rankTop8=[];
            this.loadingText = '无该指标权限';
            this.shake = false
            return
          }
          let rank8 = [];
          for (let i = 0; i < data.data.top_list.length; i++) {
            let model = {};
            model.name = data.data.top_list[i].case_info_name;
            model.value = data.data.top_list[i][this.xykpis[0]]
            model.unit = this.global.unit[this.xykpis[0]]
            rank8.push(model);
          }
          this.rankTop8=rank8;
          this.$nextTick(()=>{
            this.loading = false;
          })
        }
        else{
          this.rankTop8=[];
          this.loadingText = '无数据';
        }
        this.shake = false
      })
      .catch(err=>{
         console.log(err);
        this.rankTop8=[];
        this.loadingText = '无数据';
        this.shake = false
      })
    },
    
  },
  watch: {
    GetData:{
      handler(val){
        if(this.shake && val.index != 0){
          return
        }
        if(val){
          this.realData = val.realHeaderData;
          this.global = val.global;
          this.loading = true;
          this.shake = true
          if(!this.realData.company_ids.length){
            this.rankTop8=[];
            this.loadingText = '无数据';
            this.shake = false
            return
          }else{
            this.loadingText = '加载中...';
            this.getData();
          }
        }
      },
      deep:true

    }
  },

};
</script>
